package com.changpei.core.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.changpei.common.result.Result;
import com.changpei.core.pojo.ContentTags;
import com.changpei.core.service.ContentTagsService;
import com.changpei.core.utils.SearchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>Title : ContentTags</p>
 * <p>Description : contentTags的控制层</p>
 *
 * @author weibiao
 * @date 2020年3月7日
 */

@RestController
@RequestMapping("/contentTags")
@CrossOrigin
public class ContentTagsController {

    @Autowired
    private ContentTagsService contentTagsService;

    /**
     * <p>Title : add</p>
     * <p>Description : 新增ContentTags数据</p>
     *
     * @param contentTags
     * @return
     */
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody ContentTags contentTags) {
        // 调用ContentTagsService实现添加ContentTags
        if (contentTagsService.save(contentTags)) {
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     * <p>Title : delete</p>
     * <p>Description : ContentTags根据ID数据</p>
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<String> delete(@PathVariable Long id) {
        // 调用ContentTagsService实现根据主键删除
        if (contentTagsService.removeById(id)) {
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     * <p>Title : update</p>
     * <p>Description : ContentTags根据ID修改</p>
     *
     * @param contentTags
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result<String> update(@RequestBody ContentTags contentTags, @PathVariable Long id) {
        // 设置主键值
        contentTags.setId(id);
        // 调用ContentTagsService实现修改ContentTags
        if (contentTagsService.updateById(contentTags)) {
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     * <p>Title : findPage</p>
     * <p>Description : ContentTags分页搜索实现</p>
     *
     * @param pageNum  : 当前页
     * @param pageSize : 每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{pageNum}/{pageSize}")
    public Result<Page<ContentTags>> findPage(@PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用ContentTagsService实现分页查询ContentTags
        Page<ContentTags> pageInfo = contentTagsService.page(new Page<>(pageNum, pageSize));
        return Result.ok(pageInfo);
    }

    /**
     * <p>Title : findPage</p>
     * <p>Description : ContentTags分页条件搜索实现</p>
     *
     * @param contentTags
     * @param pageNum     : 当前页
     * @param pageSize    : 每页显示多少条
     * @return
     */
    @PostMapping(value = "/search/{pageNum}/{pageSize}")
    public Result<Page<ContentTags>> findPage(@RequestBody(required = false) ContentTags contentTags, @PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用ContentTagsService实现分页条件查询ContentTags
        Page<ContentTags> pageInfo = contentTagsService.page(new Page<>(pageNum, pageSize), SearchUtil.initQuery(contentTags));
        return Result.ok(pageInfo);
    }

    /**
     * <p>Title : findList</p>
     * <p>Description : 多条件搜索ContentTags数据</p>
     *
     * @param contentTags
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<ContentTags>> findList(@RequestBody(required = false) ContentTags contentTags) {
        // 调用ContentTagsService实现条件查询ContentTags
        List<ContentTags> list = contentTagsService.list(SearchUtil.initQuery(contentTags));
        return Result.ok(list);
    }

    /**
     * <p>Title : findById</p>
     * <p>Description : 根据ID查询ContentTags数据</p>
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result<ContentTags> findById(@PathVariable Long id) {
        // 调用ContentTagsService实现根据主键查询ContentTags
        ContentTags contentTags = contentTagsService.getById(id);
        return Result.ok(contentTags);
    }

    /**
     * <p>Title : findOne</p>
     * <p>Description : 多条件搜索ContentTags数据，结果返回一条</p>
     *
     * @param contentTags
     * @return
     */
    @PostMapping(value = "/findOne")
    public Result<ContentTags> findOne(@RequestBody(required = false) ContentTags contentTags) {
        // 调用ContentTagsService实现条件查询ContentTags
        ContentTags contentTagsResult = contentTagsService.getOne(SearchUtil.initQuery(contentTags));
        return Result.ok(contentTagsResult);
    }

    /**
     * <p>Title : findAll</p>
     * <p>Description : 查询ContentTags全部数据</p>
     *
     * @return
     */
    @GetMapping(value = "/all")
    public Result<List<ContentTags>> findAll() {
        // 调用ContentTagsService实现查询所有ContentTags
        List<ContentTags> list = contentTagsService.list();
        return Result.ok(list);
    }
}
